package com.mmy.controller.line;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.line.TLinePlan;
import com.mmy.domain.line.VLineBase;
import com.mmy.domain.line.VLinePlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.line.LineBaseServiceImpl;
import com.mmy.service.impl.line.LinePlanServiceImpl;


@Controller
public class LinePlanController {

	@Autowired
	LinePlanServiceImpl linePlanServiceImpl;
	@Autowired
	LineBaseServiceImpl lineBaseServiceImpl;
	@Autowired
	Common common;
	private static Logger logger = LoggerFactory.getLogger(LinePlanController.class);
		
	/**
	 * 函数功能说明： 进入创建线路计划单页面
	 *
	 * @param 参数说明 vLineBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 修改人 shangzl
	 * @date 创建日期 2020.08.18 修改时间 2020.11.18
	 */
	@RequestMapping(value = "/tocreatesurroudingplan")
	public String toCreatSurroudingPlan(Model model,HttpSession session,HttpServletRequest request) {
		
		int iType = Integer.parseInt(request.getParameter("type"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<VLineBase> lstLineBase = lineBaseServiceImpl.findSurrouding(tSysUser.getOrgID(),iType);
		model.addAttribute("lstLineBase", lstLineBase);
		return "webbackground/touristroutemanagement/CreateSurroudingPlan";
	}

	/**
	 * 函数功能说明： 提交--新建近郊线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 liys
	 * @date 创建日期 修改时间 2021.02.01
	 */
	@RequestMapping(value = "/savecreatesurroudingplan")
	public String saveCreateSurroundingPlan(TLinePlan tLinePlan, String startTime, String endTime, HttpSession session,HttpServletRequest request) {
		
		Integer iLineID = Integer.parseInt(request.getParameter("LineID"));
		Calendar cal = Calendar.getInstance();
		cal.setTime(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 1);
		tLinePlan.setStartDate(cal.getTime());
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD));
		cal2.set(Calendar.HOUR_OF_DAY, 23);
		cal2.set(Calendar.MINUTE, 59);
		cal2.set(Calendar.SECOND, 59);
		tLinePlan.setEndDate(cal2.getTime());
		tLinePlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_LINE_PLAN));
		tLinePlan.setCreateDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		tLinePlan.setCreator(tSysUser.getUserName());
		linePlanServiceImpl.save(tLinePlan.getPlanID());
		linePlanServiceImpl.save(tLinePlan);
		return "redirect:surroudingplan?iLineID=" + iLineID ;
	}

	/**
	 * 函数功能说明： 新建境内线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.18
	 */

	@RequestMapping(value = "/savecreatedomesticplan")
	public String saveCreateDomesticPlan(TLinePlan tLinePlan, String startTime, String endTime, HttpSession session){

		tLinePlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tLinePlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tLinePlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_LINE_PLAN));
		tLinePlan.setCreateDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		tLinePlan.setCreator(tSysUser.getUserName());
		linePlanServiceImpl.save(tLinePlan.getPlanID());
		linePlanServiceImpl.save(tLinePlan);
		return "redirect:domesticplan";
	}

	/**
	 * 函数功能说明： 新建境外线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.18
	 */

	@RequestMapping(value = "/savecreateoutboundplan")
	public String saveCreateOutboundPlan(TLinePlan tLinePlan, String startTime, String endTime, HttpSession session)
			throws Exception {

		tLinePlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tLinePlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tLinePlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_LINE_PLAN));
		try {

			tLinePlan.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tLinePlan.setCreator(tSysUser.getUserName());
			linePlanServiceImpl.save(tLinePlan.getPlanID());
			linePlanServiceImpl.save(tLinePlan);
			return "redirect:outboundplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 新建目的地线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.18
	 */

	@RequestMapping(value = "/savecreatedestinationplan")
	public String saveCreateDestinationPlan(TLinePlan tLinePlan, String startTime, String endTime, HttpSession session)
			throws Exception {

		tLinePlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tLinePlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tLinePlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_LINE_PLAN));
		try {
			tLinePlan.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tLinePlan.setCreator(tSysUser.getUserName());
			linePlanServiceImpl.save(tLinePlan.getPlanID());
			linePlanServiceImpl.save(tLinePlan);
			return "redirect:destinationplan";
		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.18
	 */
	@PostMapping("/deletelineplanchecked")
	@ResponseBody
	public String deleteLinePlanChecked(String checkList) {

		String[] arrChecked = checkList.split(",");
		List<Integer> lstPlanID = new ArrayList<>();
		for (String str : arrChecked) {
			lstPlanID.add(Integer.parseInt(str));
		}
		linePlanServiceImpl.delLinePlan(lstPlanID);
		return "webbackground/touristroutemanagement/SurroudingPlan";
	}

	/**
	 * 函数功能说明： 修改线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人liys
	 * @date 创建日期 2020.11.18 修改时间 2021.01.27
	 */

	@RequestMapping("/saveupdatelineplan")
	public String updateLinePlan(TLinePlan tLinePlan, String startTime, String endTime, HttpSession session,HttpServletRequest request){
			
			String strLineID = request.getParameter("LineID");
			TLinePlan LinePlan = linePlanServiceImpl.TfindByPlanID(tLinePlan.getPlanID());
			LinePlan.setStandard(tLinePlan.getStandard());
			LinePlan.setLineID(tLinePlan.getLineID());
			LinePlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			LinePlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			LinePlan.setFloor(tLinePlan.getFloor());
			LinePlan.setPeer(tLinePlan.getPeer());
			LinePlan.setPrice(tLinePlan.getPrice());
			LinePlan.setNumber(tLinePlan.getNumber());
			LinePlan.setMemo(tLinePlan.getMemo());
			LinePlan.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			LinePlan.setReviser(tSysUser.getUserName());
			linePlanServiceImpl.save(LinePlan);
			return "redirect:surroudingplan?iLineID=" + strLineID;
	}

	/**
	 * 函数功能说明： 跳转计划单首页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 liys
	 * @date 创建日期 2020.11.18修改时间2021.01.20 
	 */
	@RequestMapping("/surroudingplan")
	public String toSurroudingPlan(Model model,HttpServletRequest request,HttpSession session) {
			
		int iType = Integer.parseInt(request.getParameter("type"));
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			Integer iLineID = Integer.parseInt(request.getParameter("iLineID"));
			List<VLinePlan> lstSurroudingPlan = linePlanServiceImpl.findLinePlan(iLineID);
			model.addAttribute("lstSurroudingPlan", lstSurroudingPlan);
			List<VLineBase> lstLineBase = lineBaseServiceImpl.findSurrouding(tSysUser.getOrgID(),iType);
			model.addAttribute("lstLineBase", lstLineBase);
			model.addAttribute("LineID",iLineID);
			return "webbackground/tourist/SurroudingPlan";
	}

	/**
	 * 函数功能说明： 异步--当前线路计划列表
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人  liys
	 * @date 创建日期 2021.03.09
	 */
	@RequestMapping("/planJson")
	@ResponseBody
	public String getPlanJson(HttpServletRequest request) {
		Integer iLineID = Integer.parseInt(request.getParameter("iLineID"));
		List<VLinePlan> lstPlan = linePlanServiceImpl.findLinePlan(iLineID);
		Map<String,Object> ret = new HashMap<String,Object>();
		ret.put("rows", lstPlan);
		ret.put("total", 2);
		return JSONObject.toJSONString(ret);
	}
	
	/**
	 * 函数功能说明： 跳转线路计划单详情
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 shangzl
	 * @date 创建日期 修改时间 2020.11.18
	 */
	@RequestMapping(value = "/lineplaninfo")
	public String toLinePlanInfo(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		VLinePlan vLinePlan = linePlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("vLinePlan", vLinePlan);

		return "webbackground/touristroutemanagement/LinePlanInfo";
	}

	/**
	 * 函数功能说明： 修改线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl 修改人liys
	 * @throws ParseException 
	 * @date 创建日期 2020.11.18 修改时间2021.02.03
	 */

	@RequestMapping("/toupdatelineplan")
	public String toUpdateLinePlan(HttpServletRequest request, Model model,HttpSession session) throws ParseException {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));
		int iType = Integer.parseInt(request.getParameter("type"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		VLinePlan vLinePlan = linePlanServiceImpl.findByPlanID(iPlanID);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String startDateStr = sdf.format(vLinePlan.getStartDate());
		String endDateStr = sdf.format(vLinePlan.getEndDate());
		model.addAttribute("startDateStr",startDateStr);
		model.addAttribute("endDateStr", endDateStr);
		model.addAttribute("vLinePlan",vLinePlan);
		List<VLineBase> lstLineBase = new ArrayList<VLineBase>();
		lstLineBase = lineBaseServiceImpl.findSurrouding(tSysUser.getOrgID(),iType);
		model.addAttribute("lstLineBase", lstLineBase);
		return "webbackground/touristroutemanagement/UpdateLinePlan";

	}

	/**
	 * 函数功能说明： 查询线路计划
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人 liys
	 * @date 创建日期 2020.11.18 修改日期 2021.02.03
	 */
	@RequestMapping("/querysurroudingplan")
	public String querySurroudingPlan(HttpServletRequest request, Model model,HttpSession session) {
		
		int iType = Integer.parseInt(request.getParameter("type"));
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		String strLineID = request.getParameter("LineID");
		List<VLinePlan> lstSurroudingPlan = new ArrayList<VLinePlan>();
		lstSurroudingPlan = linePlanServiceImpl.findByLineID(Integer.parseInt(strLineID));
		model.addAttribute("lstSurroudingPlan", lstSurroudingPlan);
		List<VLineBase> lstLineBase = lineBaseServiceImpl.findSurrouding(tSysUser.getOrgID(),iType);
		model.addAttribute("lstLineBase", lstLineBase);
		return "webbackground/touristroutemanagement/SurroudingPlan";
	}
	

}
